package com.atguigu.chapter08;

import com.atguigu.bean.UserBehavior;
import com.atguigu.util.AnqclnUtil;
import org.apache.flink.api.common.eventtime.WatermarkStrategy;
import org.apache.flink.api.common.functions.AggregateFunction;
import org.apache.flink.api.common.functions.ReduceFunction;
import org.apache.flink.api.common.state.AggregatingState;
import org.apache.flink.api.common.state.AggregatingStateDescriptor;
import org.apache.flink.api.common.state.ReducingState;
import org.apache.flink.api.common.state.ReducingStateDescriptor;
import org.apache.flink.api.common.typeinfo.Types;
import org.apache.flink.configuration.Configuration;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.streaming.api.functions.windowing.ProcessAllWindowFunction;
import org.apache.flink.streaming.api.windowing.assigners.SlidingEventTimeWindows;
import org.apache.flink.streaming.api.windowing.time.Time;
import org.apache.flink.streaming.api.windowing.windows.TimeWindow;
import org.apache.flink.util.Collector;

import java.time.Duration;

/**
 * Author: Pepsi
 * Date: 2023/8/14
 * Desc:
 */
public class Flink03_Project_High_Pv_process_2 {
    public static void main(String[] args) {
        Configuration conf = new Configuration();
        conf.setInteger("rest.port", 1000);
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment(conf);
        env.setParallelism(2);

        env
                .readTextFile("input/UserBehavior.csv")
                .map(line -> {
                    String[] data = line.split(",");
                    return new UserBehavior(
                            Long.valueOf(data[0]),
                            Long.valueOf(data[1]),
                            Integer.valueOf(data[2]),
                            data[3],
                            // valueOf和parseLong的区别是返回值类型，前面是包装类，后面是long，建议分开用，减少自动装箱拆箱的性能浪费
                            Long.parseLong(data[4])
                    );
                })
                .filter(ub -> "pv".equals(ub.getBehavior()))
                // 分配时间戳和水印
                .assignTimestampsAndWatermarks(
                        WatermarkStrategy
                                // 乱序程度为3s
                                .<UserBehavior>forBoundedOutOfOrderness(Duration.ofSeconds(3))
                                // 指定时间戳  ，对象里面的时间戳是秒，转成毫秒
                                .withTimestampAssigner((element, recordTimestamp) -> element.getTimestamp() * 1000)
                )
                // 开窗，开一个滑动窗口，窗口大小2小时，滑动步长1小时
                .windowAll(SlidingEventTimeWindows.of(Time.hours(2), Time.hours(1)))
                .process(new ProcessAllWindowFunction<UserBehavior, String, TimeWindow>() {

                    private ReducingState<Long> pvState;

                    @Override
                    public void open(Configuration parameters) throws Exception {
                        // 描述器的泛型是指状态中存放的值
                        pvState = getRuntimeContext().getReducingState(new ReducingStateDescriptor<Long>(
                                "pvState",
                                new ReduceFunction<Long>() {
                                    @Override
                                    public Long reduce(Long value1, Long value2) throws Exception {
                                        return value1+value2;
                                    }
                                },
                                Types.LONG
                        ));
                    }

                    @Override
                    public void process(Context context,
                                        Iterable<UserBehavior> elements,
                                        Collector<String> out) throws Exception {

                        // 注意，用之前要清除一下状态里面的值，如果不清除的话就是累加了，将第二个窗口的值和第一个窗口的值加了
                        pvState.clear();

                        for (UserBehavior element : elements) {
                            // 有一个元素就向状态中 +1
                            pvState.add(1L);
                        }
                        String startTime = AnqclnUtil.toDateTime(context.window().getStart());
                        String endTime = AnqclnUtil.toDateTime(context.window().getEnd());

                        out.collect(startTime+ " - "+endTime+ " - "+pvState.get());
                    }
                })
                .print();


        try {
            env.execute();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
